from langchain_core.tools import tool, ToolException, StructuredTool


def get_weather(city: str) -> str:
    raise ToolException(f"错误：没有名为{city}的城市")


def _handle_error(error: ToolException) -> str:
    return f"调用工具时出错：{error.args[0]}"


get_weather_tool = StructuredTool.from_function(func=get_weather,
                                                description="获取天气信息",
                                                name="get_weather",
                                                # handle_tool_error=True
                                                # handle_tool_error="没有找到这个城市"
                                                handle_tool_error=_handle_error
                                                )

resp = get_weather_tool.invoke({"city": "上海"})

print(resp)
